import Vue from 'vue'
import Cookies from 'js-cookie'
import 'normalize.css/normalize.css' // a modern alternative to CSS resets
import Element from 'element-ui'
import './styles/element-variables.scss'
// import enLang from 'element-ui/lib/locale/lang/en'// 如果使用中文语言包请默认支持，无需额外引入，请删除该依赖
import '@/styles/index.scss' // global css
import App from './App'
import store from './store'
import router from './router'
import './icons' // icon
import './permission' // permission control
import * as filters from './filters' // global filters
import elDragDialog from '@/directive/el-drag-dialog'
import onlyNum from '@/directive/onlyNum'

import DataTable from '@/components/DataTable'
import MyImage from '@/components/MyImage'
import Print from 'vue-print-nb'
/*引入 icon*/
import './assets/icon/iconfont.css'

Vue.use(Print)

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
  // locale: enLang // 如果使用中文，无需设置，请删除
})

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false

// 数字输入框验证指令
Vue.directive('only-num', onlyNum)
// 弹窗拖动
Vue.directive('el-drag-dialog', elDragDialog)
// 数据表格
Vue.component('data-table', DataTable)
// 图片放大
Vue.component('MyImage', MyImage)

/*
  获取图片url
 */
Vue.prototype.$getImgUrl = function(uri = '') {
  if (uri) {
    if (uri.substr(0, 4) === 'http' || uri.substr(0, 4) === 'blob') {
      return uri
    } else {
      return process.env.VUE_APP_BASE_IMAGE_URL + uri
    }
  }
  return ''
}

Vue.prototype.$downloadFile = function(res, filename = '下载.xls') {
  const disposition = res.headers['content-disposition']
  if (disposition) {
    // 拿到文件名
    filename = decodeURIComponent(disposition.split('filename=')[1])
  }
  const blob = new Blob([res.data])
  // 兼容ie
  if ('msSaveOrOpenBlob' in navigator) {
    window.navigator.msSaveOrOpenBlob(blob, filename)
  } else {
    const URL = window.URL || window.webkitURL
    const objectUrl = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = objectUrl
    a.download = filename
    a.click()
    URL.revokeObjectURL(objectUrl)
  }
}

/**
 * 关闭标签
 * @param callback
 */
Vue.prototype.$delView = function(isBack = true, callback) {
  this.$store.dispatch('tagsView/delView', this.$route).then(() => {
    callback && callback()
    isBack && this.$router.go(-1)
  })
}

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
